package com.wzq.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wzq.bean.UserModel;
import com.wzq.service.UserService;
import com.wzq.shiro.ShiroUtils;
import com.wzq.util.ApiException;
import com.wzq.util.ApiResponse;
import com.wzq.util.Pager;
import com.wzq.util.ResponseCode;
import com.wzq.util.UtilString;

/**  
 * 管理员控制器 
 * @ClassName: UserController    
 * @author WANGJUNJIE    
 * @date 2016年8月20日 上午10:18:15    
 * @version  v 1.0    
 */
@Controller
@RequestMapping("system/user/")
public class UserController{
	@Autowired
	private UserService userService;


	@RequestMapping("dataList")
	@ResponseBody
	public ApiResponse<List<UserModel>> dataList(UserModel model,HttpServletRequest request,Pager pager)throws ApiException{
		ApiResponse<List<UserModel>> response = new  ApiResponse<List<UserModel>>(ResponseCode.SUCC);       
		List<UserModel> list = userService.queryList(model, pager);
		response.setData(list);	
		return response;
	}
	
	
	@RequestMapping("add")
	@ResponseBody
	public ApiResponse<String> add(UserModel model,HttpServletRequest request)throws ApiException{
		ApiResponse<String> response = new  ApiResponse<String>(ResponseCode.SUCC);
		
		model.setPassword(UtilString.md5(model.getPassword()));
		if(null != userService.getByAccount(model.getName())){
			response.setCode(ResponseCode.EXIST.getCode());
			response.setDesc("用户名重复！");
			return response;
		}
		
		userService.add(model);
		return response;
	}
	/**]
	 *  
	 * @param userName
	 * @return
	 */
	@RequestMapping("getAccountByName")
	@ResponseBody
	public  ApiResponse<UserModel> getAccountByName(String userName){
		ApiResponse<UserModel> response = new  ApiResponse<UserModel>(ResponseCode.SUCC);
		UserModel user =userService.getAccountByName(userName);
		response.setData(user);
		return response;
	}
	
	/**
	 * 修改密码
	 * @param npwd
	 * @param request
	 * @return
	 * @throws ApiException
	 */
	@RequestMapping("updatePassword")
	@ResponseBody
	public ApiResponse updatePassword(String npwd,HttpServletRequest request)throws ApiException{
		ApiResponse response = new ApiResponse(ResponseCode.SUCC);
		UserModel userModel =ShiroUtils.getLoginUser();
	    userModel.setPassword(UtilString.md5(npwd));
		int count = userService.updateBySelective(userModel);
		if(count<=0){
			 response = new ApiResponse(ResponseCode.ERR);
		}
		return response;
	}
	public static void main(String[] args){
		String password = "123456";
		System.out.println(UtilString.md5(password));
	}
}
